package de.hauschild.gmltracer.tracer.impl.tier1;

import java.util.List;

import org.apache.commons.math.geometry.Vector3D;

import de.hauschild.gmltracer.tracer.Intersection;
import de.hauschild.gmltracer.tracer.Ray;
import de.hauschild.gmltracer.tracer.Shape;
import de.hauschild.gmltracer.tracer.SurfaceFunction;
import de.hauschild.gmltracer.tracer.impl.AbstractShape;

public class Plane extends AbstractShape {

  public Plane(final SurfaceFunction theSurfaceFunction) {
    super(theSurfaceFunction);
  }

  @Override
  public Intersection intersectAfterIgnoration(final Ray ray, final List<Shape> shapesToIgnore) {
    final Vector3D position = worldToObject(ray.getStart());
    final Vector3D direction = worldToObject(ray.getEnd()).subtract(position).normalize();

    final double denom = Vector3D.dotProduct(Vector3D.plusJ, direction);

    if (denom < 0.0) {
      final double u = Vector3D.dotProduct(Vector3D.plusJ, position.negate()) / denom;
      return new Intersection(objectToWorld(position.add(u, direction)), this, Vector3D.plusJ);
    }

    return null;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Vector3D objectCoordinates(final Vector3D theIntersection) {
    // TODO implement correct mapping
    return new Vector3D();
  }
}
